package com.link.base.base.user.controller;

import com.link.base.base.user.model.LoginLog;
import com.link.base.base.user.service.LoginLogService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 登录日志
 */
@Controller
@RequestMapping("/link/loginlog")
public class LoginLogController extends BasicController<LoginLog> {
    private static final Logger logger = LogManager.getLogger(LoginLogController.class);
    @Resource
    private LoginLogService loginLogService;

    @Override
    public BasicService<LoginLog> getBasicService() throws Exception {
        return loginLogService;
    }


    /**
     * 添加登录成功记录,提供给微信工程调用
     *
     * @author 黄龙建LoginLogController.java:
     * @date 2020年06月12日
     */
    @RequestMapping(value = "/addLoginSuccessLog", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> addLoginSuccessLog(@RequestBody LoginLog loginLog, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            loginLogService.addLoginSuccessLog(loginLog);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
            LCLogger.withException(e);
        }
        return result;
    }
}
